#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import date, datetime, timedelta
import calendar

from tack.lib import string_app
from tack.lib.jsontools import json_dumps

display_format = '%d %b %Y'
week_display_format = '%A'
ymd_format = '%Y-%m-%d'

def today(environ, start_response):
    info  = {}
    today = date.today()

    info['today'] = today.isoformat()
    info['display'] = today.strftime(display_format).upper()
    info['previous'] = (today + timedelta(days=-1)).isoformat()
    info['next'] = (today + timedelta(days=1)).isoformat()
    output = json_dumps({'info':[info]}, indent=2)
    return string_app(output)(environ, start_response)


def anyday(environ, start_response):
    info = {}
    day = environ['dispatcher.groups'][0]
    theday = datetime.strptime(day, ymd_format)

    info['next'] = (theday + timedelta(days=1)).strftime(ymd_format)
    info['display'] = theday.strftime(display_format).upper()
    info['today'] = theday.strftime(ymd_format)
    info['previous'] = (theday + timedelta(days=-1)).strftime(ymd_format)
    output = json_dumps({'info':[info]}, indent=2)
    return string_app(output)(environ, start_response)
    
def week_span(environ, start_response):
#    info = {}
    output = {}
    weekspan = {}
    day = environ['dispatcher.groups'][0]
    theday = datetime.strptime(day, ymd_format)
    oneday = timedelta(days=1)
    startday = theday
    
    while startday.weekday() != calendar.SUNDAY:
	    startday -= oneday
    

    weekspan['today_day'] =  (theday).strftime(week_display_format)
    weekspan['first_day'] = (startday).strftime(week_display_format)
    weekspan['second_day'] = (startday + timedelta(days=1)).strftime(week_display_format)
    weekspan['third_day'] = (startday + timedelta(days=2)).strftime(week_display_format)
    weekspan['fourth_day'] = (startday + timedelta(days=3)).strftime(week_display_format)
    weekspan['fifth_day'] = (startday + timedelta(days=4)).strftime(week_display_format)
    weekspan['sixth_day'] = (startday + timedelta(days=5)).strftime(week_display_format)
    weekspan['seventh_day'] = (startday + timedelta(days=6)).strftime(week_display_format)
    
    weekspan['today'] =  (theday).strftime(display_format).upper()
    weekspan['first'] = (startday).strftime(display_format).upper()
    weekspan['second'] = (startday + timedelta(days=1)).strftime(display_format).upper()
    weekspan['third'] = (startday + timedelta(days=2)).strftime(display_format).upper()
    weekspan['fourth'] = (startday + timedelta(days=3)).strftime(display_format).upper()
    weekspan['fifth'] = (startday + timedelta(days=4)).strftime(display_format).upper()
    weekspan['sixth'] = (startday + timedelta(days=5)).strftime(display_format).upper()
    weekspan['seventh'] = (startday + timedelta(days=6)).strftime(display_format).upper()
    
    weekspan['comp_today'] =  (theday).strftime(ymd_format)
    weekspan['comp_first'] = (startday).strftime(ymd_format)
    weekspan['comp_second'] = (startday + timedelta(days=1)).strftime(ymd_format)
    weekspan['comp_third'] = (startday + timedelta(days=2)).strftime(ymd_format)
    weekspan['comp_fourth'] = (startday + timedelta(days=3)).strftime(ymd_format)
    weekspan['comp_fifth'] = (startday + timedelta(days=4)).strftime(ymd_format)
    weekspan['comp_sixth'] = (startday + timedelta(days=5)).strftime(ymd_format)
    weekspan['comp_seventh'] = (startday + timedelta(days=6)).strftime(ymd_format)
        
    output['next'] = (theday + timedelta(days=1)).strftime(ymd_format)
    output['next_week'] = (theday + timedelta(days=7)).strftime(ymd_format)
    output['display'] = theday.strftime(display_format).upper()
    output['display_week'] = (theday).strftime(display_format).upper() + ' - ' + (theday + timedelta(days=6)).strftime(display_format).upper() 
    output['today'] = theday.strftime(ymd_format)
    output['previous'] = (theday + timedelta(days=-1)).strftime(ymd_format)
    output['previous_week'] = (theday + timedelta(days=-7)).strftime(ymd_format)
    output = json_dumps({'info':[output],'weekspan':[weekspan]}, indent=2)
    return string_app(output)(environ, start_response)
    
def month(environ, start_response):
    info  = {}
    today = date.today()
    info['current'] = today.strftime('%Y-%m')
    info['display'] = today.strftime('%b %Y').upper()
    if int(today.strftime('%m')) == 1:
        previous = int(today.strftime('%Y')) - 1 
        info['previous'] = str(previous) + "-12"
    else:
        previous = int(today.strftime('%m')) - 1
        if previous < 10:
            previous = "0" + str(previous)
        info['previous'] = today.strftime('%Y') + "-" + str(previous)
    if int(today.strftime('%m')) == 12:
        next_year = str(int(today.strftime('%Y')) + 1)
        next_month = "01"
        info['next'] = next_year + "-" + next_month
    else:
        next_year = today.strftime('%Y')
        next_month = int(today.strftime('%m')) + 1
        if next_month < 10:
            next_month = "0" + str(next_month)
        next = str(next_year) + "-" + str(next_month)
        info['next'] = next
    first_day_of_this_month = date(int(today.strftime('%Y')),int(today.strftime('%m')),1)
    first_day_of_next_month = date(int(next_year),int(next_month),1)
    number_of_days = first_day_of_next_month - first_day_of_this_month
    info['number_of_days'] = number_of_days.days
    output = json_dumps({'info':[info]}, indent=2)
    return string_app(output)(environ, start_response)

def anymonth(environ, start_response):
    month_string = environ['dispatcher.groups']['month']
    info  = {}
    list = month_string.split('-')
    today_year = list[0]
    today_month = list[1]
    today = date(int(today_year), int(today_month), 1)
    info['current'] = today.strftime('%Y-%m')
    info['display'] = today.strftime('%b %Y').upper()
    if int(today.strftime('%m')) == 1:
        previous = int(today.strftime('%Y')) - 1 
        info['previous'] = str(previous) + "-12"
    else:
        previous = int(today.strftime('%m')) - 1
        if previous < 10:
            previous = "0" + str(previous)
        info['previous'] = today.strftime('%Y') + "-" + str(previous)
    if int(today.strftime('%m')) == 12:
        next_year = str(int(today.strftime('%Y')) + 1)
        next_month = "01"
        info['next'] = next_year + "-" + next_month
    else:
        next_year = today.strftime('%Y')
        next_month = int(today.strftime('%m')) + 1
        if next_month < 10:
            next_month = "0" + str(next_month)
        next = str(next_year) + "-" + str(next_month)
        info['next'] = next
    first_day_of_next_month = date(int(next_year),int(next_month),1)
    number_of_days = first_day_of_next_month - today
    info['number_of_days'] = number_of_days.days
    output = json_dumps({'info':[info]}, indent=2)
    return string_app(output)(environ, start_response)
    